package cn.semyes.ylipfilter.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetHtmlSourceUtil {
	
	
	/*
	* 获取源代码
	*/
	public static String getHtmlSource(String url){

		InputStream inputStream = null;
		BufferedReader in = null;
		StringBuilder htmlSource= new StringBuilder();

		try {
			//1.获取网址
			URL u = new URL(url);
			//2.打开连接
			HttpURLConnection conn = (HttpURLConnection) u.openConnection();
			//3.获取输入流
			inputStream = conn.getInputStream();
			//4.将源代码写入内存(设置编码)
			in = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
			String str = "";
			while((str = in.readLine()) != null){
				htmlSource.append(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//关闭I/o
			try {
			if(in != null)in.close();
			if(inputStream != null)inputStream.close();
			} catch (IOException e) {
			e.printStackTrace();
			}

		}
        return htmlSource.toString();
	}

}
